﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DatabaseCompare.Model;
using DatabaseCompare.Service.Compare;

namespace DatabaseCompare.ServiceImpl.Compare
{

    public class ColumnListCompare : ICompareAbleListCompare<ColumnInfo, ColumnCompareResult>
    {

        /// <summary>
        /// 列的比较类.
        /// </summary>
        private ICompareAbleCompare<ColumnInfo, ColumnCompareResult> columnCompare = new ColumnCompare();


        public List<ColumnCompareResult> Compare(List<ColumnInfo> leftColumnInfoList, List<ColumnInfo> rightColumnInfoList) 
        {
            // 结果列表.
            List<ColumnCompareResult> resultList = new List<ColumnCompareResult>();

            // 为避免下面的 Linq 处理异常
            // 假如 一个表的  列 的列表为 null
            // 将其设置为一个 空的列表.
            if (leftColumnInfoList == null)
            {
                leftColumnInfoList = new List<ColumnInfo>();
            }
            if (rightColumnInfoList == null)
            {
                rightColumnInfoList = new List<ColumnInfo>();
            }
                

            // 使用 Linq 
            // 检索两个表的 列 的并集。
            // 同时合并掉 相同的列.
            var query =
                (from column in leftColumnInfoList
                    select column.Name
                    ).Union
                (from column in rightColumnInfoList
                    select column.Name
                    );


            // 获取 列名 List.
            List<string> columnNameList = query.ToList();

            // 列名排序.
            columnNameList.Sort();


            // 依次比较列.
            for (int i = 0; i < columnNameList.Count; i++)
            {
                // 检索左边的列.
                var queryLeft =
                    from data in leftColumnInfoList
                    where data.Name == columnNameList[i]
                    select data;

                // 检索右边的列.
                var queryRight =
                    from data in rightColumnInfoList
                    where data.Name == columnNameList[i]
                    select data;


                // 列的比较结果.
                ColumnCompareResult columnCompareResult = null;

                if (queryLeft.Count() == 0)
                {
                    if (queryRight.Count() == 0)
                    {
                        // 左表找不到列， 右表也找不到列.
                        // 理论上这种情况不会发生.
                        columnCompareResult = columnCompare.Compare(null, null);
                    }
                    else
                    {
                        // 左表找不到列， 右表找到列.
                        columnCompareResult = columnCompare.Compare(null, queryRight.First());
                    }
                }
                else
                {
                    if (queryRight.Count() == 0)
                    {
                        // 左表找到列， 右表找不到列.
                        columnCompareResult = columnCompare.Compare(queryLeft.First(), null);
                    }
                    else
                    {
                        // 左表找到列， 右表找到列.
                        columnCompareResult = columnCompare.Compare(queryLeft.First(), queryRight.First());
                    }
                }


                // 将 列的比较结果加入最后的比较列表.
                resultList.Add(columnCompareResult);

            }

            return resultList;
        }

    }


}
